package com.beep.erp.model.stockIn;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.beep.erp.Constants;
import com.beep.erp.R;
import com.beep.erp.common.base.ToolbarActivity;
import com.beep.erp.common.bean.StockInListBean;
import com.beep.erp.common.utils.MD5util;
import com.beep.erp.common.utils.TimeUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.recyclerview.divider.HorizontalDividerItemDecoration;
import com.beep.erp.common.widget.refresh.RefreshView;
import com.beep.erp.common.widget.refresh.container.DefaultFooter;
import com.beep.erp.common.widget.refresh.container.DefaultHeader;
import com.beep.erp.common.widget.tips.TipsHelper;
import com.beep.erp.model.stockIn.stockindetail.StockInDetailActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * 入库单列表
 */
public class StockInListActivity extends ToolbarActivity<StockInListPresenter, StockInListModel> implements StockInListContract.View, RefreshView.OnFreshListener,
        StockInListAdapter.OnItemClickLitener {

    @BindView(R.id.appbatlayout)
    AppBarLayout appbatlayout;
    @BindView(R.id.rl_instock)
    RecyclerView rlInstock;
    @BindView(R.id.super_refresh)
    RefreshView superRefresh;
    @BindView(R.id.activity_invite)
    CoordinatorLayout activityInvite;
    @BindView(R.id.tv_right)
    TextView tvRight;

    TipsHelper mTipsHelper;
    private ArrayList<StockInListBean.BizStockInVoListBean> mListBeen = new ArrayList<>();
    private StockInListAdapter mStockListAdapter;
    private int mPageNum = 1;

    public static void start(Context context) {
        Intent intent = new Intent(context, StockInListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean isBack() {
        return true;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_intstock_goods;
    }

    @Override
    protected void initView() {
        getTvTitle().setText(R.string.stock_in);

        mTipsHelper = createTipsHelper(superRefresh);
        //refreshView下拉刷新 上拉加载
        superRefresh.setListener(this);
        superRefresh.setHeader(new DefaultHeader(this, true));
        superRefresh.setFooter(new DefaultFooter(this, true));
        //mInviteAdapter recycleview适配器
        mStockListAdapter = new StockInListAdapter(this, mListBeen);
        //设置为纵向
        rlInstock.setLayoutManager(new LinearLayoutManager(this));
        //必写
        rlInstock.setHasFixedSize(true);
        //添加分割线
        rlInstock.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
        rlInstock.setAdapter(mStockListAdapter);

        mStockListAdapter.setOnItemClickLitener(this);

    }

    @Override
    public void getDate() {
        mListBeen.clear();
        mPageNum = 1;
        mTipsHelper.hideEmptyType();
        mTipsHelper.hideError();
        mTipsHelper.showLoading(true);
        getStockDetail();
    }

    @Override
    public void getStockInList(StockInListBean stockInListBean) {
        //请求成功
        if (ToolsUtils.isEmptyList(stockInListBean.getBizStockInVoList()) && mPageNum > 1) {
            ToastUtils.showShort(getString(R.string.no_data));
            return;
        }
        mListBeen.addAll(stockInListBean.getBizStockInVoList());
        mStockListAdapter.notifyDataSetChanged();
        if (stockInListBean.getBizStockInVoList().isEmpty()) {
            mTipsHelper.showEmptyType(getString(R.string.emptymessage15));
        }
    }

    private void getStockDetail() {
        long mills = TimeUtils.getCurTimeMills();

        Map<String, String> map = new HashMap<>();
        map.put("pageSize", Constants.PAGE_SIZE + "");
        map.put("pageNo", mPageNum + "");
        mPresenter.getStockInList(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", Constants.PAGE_SIZE, mPageNum);
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
        mTipsHelper.hideLoading();
        mTipsHelper.hideEmptyType();
        mTipsHelper.hideEmpty();
        mTipsHelper.hideError();
        superRefresh.onFinishFreshAndLoad();
    }

    @Override
    public void showMessage(String message) {
        ToastUtils.showShort(message);
    }

    //刷新
    @Override
    public void onRefresh() {
        getDate();
    }

    @Override
    public void onLoadmore() {
        mPageNum++;
        getStockDetail();
    }

    @Override
    public void onItemClick(View view, int position) {
        StockInDetailActivity.start(this, mListBeen.get(position).getStockInId());
    }
}
